package com.app.controller;

import com.app.model.LoanInfoModel;;
import com.app.service.LoanInfoService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.LoanInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//贷款信息
@RestController
@Controller
@RequestMapping("/jiuquan/loanInfo")
public class LoanInfoController {
    @Autowired
    private LoanInfoService loanInfoService;

    //查询列表
    @GetMapping("/getLoanInfoList")
    public Result getLoanInfoList(@RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "10") Integer limit,String title,Double maxMoney) {
        LoanInfoVo loanInfoVo = loanInfoService.getLoanInfoList(page, limit,title,maxMoney);
        return Result.success(loanInfoVo);
    }

    //详情
    @GetMapping("/getLoanInfoById")
    public Result getLoanInfoById(Integer id) {
        LoanInfoModel loanInfo = loanInfoService.getById(id);
        return Result.success(loanInfo);
    }

    //用户
//    @GetMapping("/getLoanInfoByUser")
//    public Result getLoanInfoByUser(HttpServletRequest request) {
//        String openId="asdadasdads";
//        LoanInfoModel loanInfo = loanInfoService.getLoanInfoByUser(openId);
//        return Result.success(loanInfo);
//    }

    //用户新增和修改
    @PostMapping("/addAndUpdateLoanInfo")
    public Result addAndUpdateLoanInfo(@RequestBody LoanInfoModel loanInfoModel) {
        LoanInfoModel loanInfo = loanInfoService.getById(loanInfoModel.getId());
        if (loanInfo != null) {
            loanInfoModel.setUpdateTime(CommonHelp.getNowTimestamp());
            loanInfoService.updateById(loanInfoModel);
            return Result.success("修改成功");
        } else {
            loanInfoModel.setCreateTime(CommonHelp.getNowTimestamp());
            loanInfoService.save(loanInfoModel);
            return Result.success("添加成功");
        }
    }

    //用户新增和修改
//    @PostMapping("/userAddAndUpdateLoanInfo")
//    public Result addAndUpdateLoanInfo(@RequestBody LoanInfoModel loanInfoModel, HttpServletRequest request) {
//        String token = request.getHeader("token");
//        if (token == null) {
//            return Result.error(405, "token不存在");
//        }
//        String openId = TokenUtils.verify(token);
//        UserModel user = userService.getUserByOpenid(openId);
//        if (user == null) {
//            return Result.error(406, "用户不存在");
//        }
//        QueryWrapper<LoanInfoModel> qw =new QueryWrapper<>();
//        qw.eq("open_id",openId);
//        LoanInfoModel loanInfo = loanInfoService.getOne(qw);
//        if (loanInfo != null) {
//            loanInfoModel.setUpdateTime(CommonHelp.getNowTimestamp());
//            loanInfoService.updateById(loanInfoModel);
//            return Result.success("修改成功");
//        } else {
//            loanInfoModel.setOpenId(openId);
//            loanInfoModel.setCreateTime(CommonHelp.getNowTimestamp());
//            loanInfoService.save(loanInfoModel);
//            return Result.success("添加成功");
//        }
//    }

    //详情
    @GetMapping("/delLoanInfo")
    public Result delLoanInfo(Integer id) {
        loanInfoService.removeById(id);
        return Result.success("删除成功");
    }
}
